package com.student.dms;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.utils.SpUtils;
import com.luck.picture.lib.utils.ToastUtils;
import com.student.db.AccountDao;
import com.student.db.AccountEntity;
import com.student.db.AppDatabase;
import com.student.db.PunchDao;
import com.student.db.PunchEntity;
import com.student.db.RoomDao;
import com.student.db.ScoreDao;
import com.student.dms.databinding.ActivityEditDayScoreBinding;
import com.student.dms.databinding.ActivityPunchInAndOutBinding;

import java.text.SimpleDateFormat;
import java.util.Date;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class ActivityPunchInAndOut extends BaseActivity {


    private AppDatabase roomDatabase;
    private ActivityPunchInAndOutBinding activityPunchInAndOutBinding;
    private AccountEntity accountEntity;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        roomDatabase = DMSApplication.getApplication().getRoomDatabase();
        PunchDao punchDao = roomDatabase.providePunchDao();
        activityPunchInAndOutBinding = ActivityPunchInAndOutBinding.inflate(getLayoutInflater());
        setContentView(activityPunchInAndOutBinding.getRoot());
        SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", "");
        AppDatabase roomDatabase = DMSApplication.getApplication().getRoomDatabase();
        AccountDao accountDao = roomDatabase.provideAccountDao();
        Single<AccountEntity> accountFlow = accountDao.searchAccountByName(username);
        disposable.add(accountFlow.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(accountEntity -> {
            this.accountEntity=accountEntity;
        }));

        activityPunchInAndOutBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(TitleBar titleBar) {
                finish();
            }
        });

        activityPunchInAndOutBinding.come.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ActivityPunchInAndOut.this)
                        .setCancelable(false);
                EditText input = new EditText(ActivityPunchInAndOut.this);
                input.setPadding(dip2px(ActivityPunchInAndOut.this, 23f), dip2px(ActivityPunchInAndOut.this, 10f), 0, dip2px(ActivityPunchInAndOut.this, 10f));
                builder.setTitle("出寝打卡");
                input.setHint("请输入原因");
                builder.setView(input);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 获取输入框的内容
                        String inputText = input.getText().toString().trim();
                        PunchEntity punchEntity = new PunchEntity();
                        punchEntity.setRoomNo(accountEntity.getRoomNo());
                        punchEntity.setType(1);
                        punchEntity.setName(accountEntity.getName());
                        punchEntity.setReason(inputText);
                        punchEntity.setPunchTime(new Date().getTime());
                        disposable.add(punchDao.insert(punchEntity).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(() -> {
                            ToastUtils.showToast(ActivityPunchInAndOut.this, "出寝打卡成功！");
                            dialog.dismiss();
                        }));
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.show();
            }
        });

        activityPunchInAndOutBinding.income.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ActivityPunchInAndOut.this)
                        .setCancelable(false);
                EditText input = new EditText(ActivityPunchInAndOut.this);
                input.setPadding(dip2px(ActivityPunchInAndOut.this, 23f), dip2px(ActivityPunchInAndOut.this, 10f), 0, dip2px(ActivityPunchInAndOut.this, 10f));
                builder.setTitle("归寝打卡");
                input.setHint("请输入原因");
                builder.setView(input);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 获取输入框的内容
                        String inputText = input.getText().toString().trim();
                        PunchEntity punchEntity = new PunchEntity();
                        punchEntity.setRoomNo(accountEntity.getRoomNo());
                        punchEntity.setType(0);
                        punchEntity.setName(accountEntity.getName());
                        punchEntity.setReason(inputText);
                        punchEntity.setPunchTime(new Date().getTime());
                        disposable.add(punchDao.insert(punchEntity).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(() -> {
                            ToastUtils.showToast(ActivityPunchInAndOut.this, "归寝打卡成功！");
                            dialog.dismiss();
                        }));
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.show();
            }
        });
    }


    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
